/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cpasl;

import java.awt.Point;
import java.io.File;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.Random;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.control.SkinBase;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Screen;

/**
 *
 * @author seanm_000
 */
public class VideoGroup extends Group {
    
    protected static final int MARGIN = 10;
    
    protected static final int VIDEO_WIDTH = 320;
    
    protected static final int VIDEO_HEIGHT = 240;
    
    protected static final int LABEL_Y_OFFSET = 40;
    
    protected Media media = null;
    
    protected MediaPlayer mediaPlayer = null;
    
    protected MediaView mediaView = null;

    /** Used to generate random numbers. */
    private Random random = new Random(Calendar.getInstance().getTimeInMillis());
    
    protected int centerX = 0;
    
    protected int centerY = 0;
    
    protected Label label = new Label();
    
    
    public VideoGroup() {  
        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
        centerX = (int) (primaryScreenBounds.getWidth()/2) - (VIDEO_WIDTH/2);
        centerY = (int) (primaryScreenBounds.getHeight()/2) - (VIDEO_HEIGHT/2);
    }

    public void loadVideo(int videoIndex, boolean x) {     // create media player
        File mp4File = CpaslManager.getInstance().getVideoFile(videoIndex, x);
        loadVideo(mp4File);
    }
    
    public void setLabel(String labelText) {
        label.setText(labelText);
        if (labelText.isEmpty()) {
            if (getChildren().contains(label)) {
                getChildren().remove(label);
            }
        }
        else {
            if (!getChildren().contains(label)) {
                getChildren().add(label);
            }
        }
        
    }
    
    public void loadVideo(File file) {
        double x = 0;
        double y = 0;
        if (mediaView != null) {
            x = mediaView.getX();
            y = mediaView.getY();
            getChildren().remove(mediaView);
        }
        try {
            media = new Media(file.toURI().toURL().toString());
            mediaPlayer = new MediaPlayer(media);
            // create mediaView and add media player to the viewer
            mediaView = new MediaView(mediaPlayer);
            mediaView.setX(x);
            mediaView.setY(y);
            getChildren().add(mediaView);
        }
        catch (MalformedURLException mue) {
            mue.printStackTrace();
        }
    }
            
    public MediaPlayer getMediaPlayer() {
        return mediaPlayer;
    }
    
    public void centerVideo() {
        if (getChildren().contains(label)) {
            label.setTranslateX((double) centerX);
            label.setTranslateY((double) (centerY - LABEL_Y_OFFSET));
        }
        mediaView.setX(centerX);
        mediaView.setY(centerY);
    }
    
    public void randomizeVideoLocation() {    
        boolean labelActive = getChildren().contains(label);
        int x = (int) mediaView.getX();
        int y = (int) mediaView.getY();
        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
        int yBound = (int) primaryScreenBounds.getHeight() - ((2 * MARGIN) + VIDEO_HEIGHT);
        if (labelActive) {
            yBound -= LABEL_Y_OFFSET;
        }
        int newX = MARGIN + random.nextInt((int) primaryScreenBounds.getWidth() - ((2 * MARGIN) + VIDEO_WIDTH));
        int newY = MARGIN + random.nextInt(yBound);
        if (labelActive) {
            newY += LABEL_Y_OFFSET;
        }
        while (Point.distance(newX, newY, x, y) < (int) media.getWidth()) {
            newX = MARGIN + random.nextInt((int) primaryScreenBounds.getWidth() - ((2 * MARGIN) + VIDEO_WIDTH));
            newY = MARGIN + random.nextInt(yBound);
            if (labelActive) {
                newY += LABEL_Y_OFFSET;
            }
        }
        if (labelActive) {
            label.setTranslateX((double) newX);
            label.setTranslateY((double) (newY - LABEL_Y_OFFSET));
        }
        mediaView.setX((double) newX);
        mediaView.setY((double) newY);
        mediaPlayer.setOnEndOfMedia(new Runnable() {

            @Override
            public void run() {
                mediaPlayer.stop();
                CpaslManager.getInstance().switchToDisplay(Groups.CHOICE, 500);
            }
        });
    }
}
